package org.mtmi.ui.scenes;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;
import org.mtmi.midi.GM;
import org.mtmi.midi.MidiManager;
import org.mtmi.midi.SendMidiCCButton;
import org.mtmi.midi.SendMidiNoteButton;
import org.mtmi.ui.M;
import org.mtmi.ui.controls.Label;
import org.mtmi.ui.controls.LedBargraph;
import org.mtmi.ui.controls.Slider;
import org.mtmi.ui.controls.Spinner;
import org.mtmi.ui.controls.listeners.ValueChangedListener;
import org.mtmi.ui.controls.listeners.ValueEvent;
import org.mtmi.ui.controls.midi.MidiController;
import org.mtmi.ui.controls.midi.MidiKeyboard;
import org.mtmi.ui.links.CID;



public class MidiScene extends Scene {

	public MidiScene(Shell parent) {
		super(parent);
		Rectangle ca = parent.getClientArea();
		setSize(ca.width, ca.height);
		init();
	}
	
	private void init() {
		
		createMasterButton1();
		createMasterButton2();
		createMasterButton3();
		createMasterButton4();
		createMidiController();
		createStopButton();
		createPlayButton();
		
		createNoteButton();
		createLedTest();
	}
	
	private void createLedTest() {
		LedBargraph led = new LedBargraph( this, M.VERTICAL );
		led.setBounds( 600, 50, 30, 300 );
		Slider slider = new Slider( this, false, 0, 0, 100, M.SCENE_CONTROL );
		slider.setBounds( 650, 50, 60, 300 );
		
		led.setValueController( slider, CID.VALUE_GENERATOR );
	}
	
	private void createMidiController()
	{
		MidiController mc = new MidiController( this, M.SCENE_CONTROL );
		mc.setLocation( 560, 125 );
	}

	private void createNoteButton() {
		
		SendMidiNoteButton button1 = new SendMidiNoteButton(this, 0, 60, 127);
		button1.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		button1.setText("Play C3");
		button1.setLocation(50, 500);
		
		Spinner spd = new Spinner( this, 0, -5, +5, M.SCENE_CONTROL );
		
		int y = button1.getLocation().y+button1.getSize().y+20;
		spd.setBounds( 50, y, button1.getSize().x, button1.getSize().y );
		
		int r = button1.getLocation().x+button1.getSize().x;
		final MidiKeyboard kb = new MidiKeyboard( this, M.SCENE_CONTROL );
		kb.setOctaveCount(2);
		kb.setSize( new Point( kb.getPreferredWidthForHeight(170), 170) );
		kb.setLocation( r+20, 500 );
		
		kb.setTransposeController( spd, null );
		
		//instrument
		Spinner spi = new Spinner( this, 0, 0, 127, M.SCENE_CONTROL );
		Rectangle rc = spd.getBounds(null);
		spi.setBounds( rc.x, rc.y+rc.height+20, rc.width, rc.height );
		
		//display GM instrument name:
		final Label li = new Label( this, M.SCENE_CONTROL|M.VCENTER );
		rc = spi.getBounds( rc );
		li.setBounds( rc.x+rc.width+20, rc.y, 200, rc.height );
		
		spi.addValueChangedListener( new ValueChangedListener() {
			@Override
			public void valueChanged(ValueEvent event) {
				int instr = event.newValue;
				li.setText( ">"+GM.Instruments[ instr ] );
				li.getScene().redraw();//text may exceed label bounds
				kb.setMidiInstrument( instr );
			}
		});
		li.setText( ">"+GM.Instruments[ spi.getValue() ] );
	}

	private void createPlayButton() {
		SendMidiCCButton playButton = new SendMidiCCButton(this, 0, 63, 0, 127);
		playButton.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		playButton.setText("Play");
		playButton.setLocation(50, 50);
	}

	private void createStopButton() {
		SendMidiCCButton stopButton = new SendMidiCCButton(this, 0, 62, 0, 127);
		stopButton.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		stopButton.setText("Stop");
		stopButton.setLocation(175, 50);
	}

	private void createMasterButton1() {
		SendMidiCCButton masterButton1 = new SendMidiCCButton(this, 0, 60, 0, 127);
		masterButton1.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		masterButton1.setText("Master1");
		masterButton1.setLocation(350, 50);
		
	}
	
	private void createMasterButton2() {
		SendMidiCCButton masterButton2 = new SendMidiCCButton(this, 0, 61, 0, 127);
		masterButton2.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		masterButton2.setText("Master2");
		masterButton2.setLocation(350, 125);
		
	}
	
	private void createMasterButton3() {
		SendMidiCCButton masterButton3 = new SendMidiCCButton(this, 0, 64, 0, 127);
		masterButton3.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		masterButton3.setText("Master3");
		masterButton3.setLocation(350, 200);
		
	}
	
	private void createMasterButton4() {
		SendMidiCCButton masterButton4 = new SendMidiCCButton(this, 0, 65, 0, 127);
		masterButton4.setMidiOutput(MidiManager.getInstance().getMidiOutput(0));
		masterButton4.setText("Master4");
		masterButton4.setLocation(350, 275);
		
	}

	
	
	
	

}
